package com.interfaces.module.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.interfaces.aspect.auth.LoginAuth;
import com.interfaces.enumeration.HTTP_STATUS_CODE_ENUM;
import com.interfaces.module.user.domain.FtsUser;
import com.interfaces.util.Result;
import com.interfaces.util.TokenUtil;
import com.ruoyi.common.core.domain.BaseEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

public abstract class CommonController<T extends BaseEntity> {
    protected abstract IService<T> getCoreService();

    @GetMapping("/listPage")
    @ResponseBody
    public Result listPage(Integer page, Integer pageSize, String where) {
        Page<T> pageParams = new Page<T>(page, pageSize);
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
        if (StringUtils.isNotEmpty(where)) {
            if (where.contains("&")) {
                String[] whereDatas = where.split("&");
                for (String whereStr : whereDatas) {
                    String[] whereParam = whereStr.split("=");
                    queryWrapper.eq(whereParam[0], whereParam[1]);
                }
            } else {
                if (where.contains("=")) {
                    String[] whereParam = where.split("=");
                    queryWrapper.eq(whereParam[0], whereParam[1]);
                } else if (where.contains("=")) {
                    String[] whereParam = where.split("like");
                    queryWrapper.like(whereParam[0].trim(), whereParam[1].trim());
                }

            }
        }
        queryWrapper.orderByDesc("create_time");
        IPage<T> pageData = getCoreService().page(pageParams, queryWrapper);
        return Result.SUCCESS(pageData.getRecords());
    }

    @GetMapping("/list")
    @ResponseBody
    public Result list(String where) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
        if (StringUtils.isNotEmpty(where)) {
            if (where.contains("&")) {
                String[] whereDatas = where.split("&");
                for (String whereStr : whereDatas) {
                    String[] whereParam = whereStr.split("=");
                    queryWrapper.eq(whereParam[0], whereParam[1]);
                }
            } else {
                if (where.contains("=")) {
                    String[] whereParam = where.split("=");
                    queryWrapper.eq(whereParam[0], whereParam[1]);
                } else if (where.contains("=")) {
                    String[] whereParam = where.split("like");
                    queryWrapper.like(whereParam[0].trim(), whereParam[1].trim());
                }

            }
        }
        queryWrapper.orderByDesc("create_time");
        List<T> dataList = getCoreService().list(queryWrapper);
        return Result.SUCCESS(dataList);
    }

    @LoginAuth(flag = true, remark = "数据增加操作")
    @PostMapping("/addData")
    @ResponseBody
    public Result addData(@RequestBody T t) {
        FtsUser ftsUser = TokenUtil.getTokenUser();
        t.setCreateTime(new Date());
        t.setCreateBy(ftsUser.getNickName());
        t.setCreateUserId(ftsUser.getId());
        getCoreService().save(t);
        return Result.SUCCESS(t, HTTP_STATUS_CODE_ENUM.SUCCESS_STATUS.getMessage());
    }

    @LoginAuth(flag = true, remark = "数据更新操作")
    @PutMapping("/updateData")
    @ResponseBody
    public Result updateData(@RequestBody T t, String where) {
        FtsUser ftsUser = TokenUtil.getTokenUser();
        t.setUpdateTime(new Date());
        t.setUpdateBy(ftsUser.getNickName());
        t.setUpdateUserId(ftsUser.getId());
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(where)) {
            if (where.contains("&")) {
                String[] whereDatas = where.split("&");
                for (String whereStr : whereDatas) {
                    String[] whereParam = whereStr.split("=");
                    queryWrapper.eq(whereParam[0], whereParam[1]);
                }
            } else {
                String[] whereParam = where.split("=");
                queryWrapper.eq(whereParam[0], whereParam[1]);
            }
            getCoreService().update(t, queryWrapper);
        } else {
            getCoreService().updateById(t);
        }

        return Result.SUCCESS(HTTP_STATUS_CODE_ENUM.SUCCESS_STATUS);
    }

    @GetMapping("/getDataById/{id}")
    @ResponseBody
    public Result getDataById(@PathVariable Serializable id, String where) {
        T t;
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
        if (StringUtils.isNotEmpty(where)) {
            if (where.contains("&")) {
                String[] whereDatas = where.split("&");
                for (String whereStr : whereDatas) {
                    String[] whereParam = whereStr.split("=");
                    queryWrapper.eq(whereParam[0], whereParam[1]);
                }
            } else {
                String[] whereParam = where.split("=");
                queryWrapper.eq(whereParam[0], whereParam[1]);
            }

            t = getCoreService().getOne(queryWrapper);
        } else {
            t = getCoreService().getById(id);
        }
        return Result.SUCCESS(t);
    }
}
